// Function.prototype.my_bind = function(obj) {
// 	if (typeof this !== 'function') {
// 		throw new TypeError('error')
// 	}
// 	obj = obj || window

// 	const args = Array.prototype.slice.call(arguments, 1)
// 	const _this = this
// 	const pro = function() {}
// 	if (this.prototype) {
// 		pro.prototype = this.prototype
// 	}
// 	const bound = function() {
// 		// 判断该函数是否被new

// 		return _this.apply(
// 			this instanceof pro ? this : obj,
// 			args.concat(Array.prototype.slice.call(arguments))
// 		)
// 	}
// 	bound.prototype = new pro() // 继承到了foo的原型
// 	return bound
// }
// var name = 'cat'

// .......如果bound被new ， foo就变成的实例对象的构造函数
// new bound().__proto__  ===  foo.prototype
Function.prototype.my_bind = function(context, ...args1) {
	if (typeof this !== 'function') {
		throw new TypeError('error')
	}
	context = context || window
	const _this = this
	const help = function() {}
	if (this.prototype) {
		help.prototype = this.prototype
	}
	const bound = function(...args2) {
		return _this.call(
			this instanceof help ? this : context,
			...args1,
			...args2)
	}
	bound.prototype = new help()
	return bound
}
var name = 'cat'
var obj = {
	name: 'dog',
	fn: foo
}

function foo(a, b) {
	console.log(this.name, a + b);
	return a + b
}
const bar = foo.my_bind(obj)
new bar(3, 4) //cat 7
